package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jsh.erp.datasource.entities.Material;
import com.jsh.erp.datasource.entities.MaterialExtend;
import com.jsh.erp.datasource.vo.MaterialExtendVo4List;
import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.materialExtend.MaterialExtendService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jijiaqing
 */
@RestController
@RequestMapping(value = "/materialsExtend")
@Api(tags = {"商品价格扩展"})
public class MaterialExtendController {
    private Logger logger = LoggerFactory.getLogger(MaterialExtendController.class);
    @Resource
    private MaterialExtendService materialExtendService;
    @Resource
    private RedisService redisService;
    @Resource
    private MaterialService materialService;
    @GetMapping(value = "/getDetailList")
    @ApiOperation(value = "价格信息列表")
    public BaseResponseInfo getDetailList(@RequestParam("materialId") String materialId,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<MaterialExtendVo4List> dataList = new ArrayList<MaterialExtendVo4List>();
            if(!StringUtil.isEmpty(materialId)) {
                dataList = materialExtendService.getDetailList(materialId);
            }
            JSONObject outer = new JSONObject();
            outer.put("total", dataList.size());
            //存放数据json数组
            JSONArray dataArray = new JSONArray();
            if (null != dataList) {
                for (MaterialExtendVo4List md : dataList) {
                    JSONObject item = new JSONObject();
                    item.put("id", md.getId());
                    item.put("barCode", md.getBarCode());
                    item.put("commodityUnit", md.getCommodityUnit());
                    if(StringUtil.isNotEmpty(md.getSku())){
                        item.put("sku", md.getSku());
                    }
                    item.put("proportion", md.getProportion());
                    item.put("purchaseDecimal", md.getPurchaseDecimal());
                    item.put("commodityDecimal", md.getCommodityDecimal());
                    item.put("wholesaleDecimal", md.getWholesaleDecimal());
                    item.put("lowDecimal", md.getLowDecimal());
                    item.put("twoWholesaleDecimal", md.getTwoWholesaleDecimal());
                    item.put("threeWholesaleDecimal", md.getThreeWholesaleDecimal());
                    item.put("fourWholesaleDecimal", md.getFourWholesaleDecimal());
                    item.put("fiveWholesaleDecimal", md.getFiveWholesaleDecimal());
                    item.put("defaultInFlag", md.getDefaultInFlag());
                    item.put("defaultOutFlag", md.getDefaultOutFlag());
                    item.put("code", md.getCode());
                    dataArray.add(item);
                }
            }
            outer.put("rows", dataArray);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * 根据商品价格id查询商品信息
     * @param meId
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getInfoByBarCode")
    @ApiOperation(value = "根据商品价格id查询商品信息")
    public BaseResponseInfo getInfoByBarCode(@RequestParam("meId") String meId,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            MaterialExtend materialExtend = materialExtendService.getInfoByMeId(meId);
            res.code = 200;
            res.data = materialExtend;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * 校验条码是否存在
     * @param id
     * @param barCode
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/checkIsBarCodeExist")
    @ApiOperation(value = "校验条码是否存在")
    public BaseResponseInfo checkIsBarCodeExist(@RequestParam("id") String id,
                                                @RequestParam("barCode") String barCode,
                                                @RequestParam("categoryId") String categoryId,
                                             HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<>();
        try {
            if (StringUtils.isEmpty(barCode)){
                map.put("status", false);
            }else {
                List<MaterialExtend> list = materialExtendService.checkIsBarCodeExist(id, barCode,categoryId);
                if(list!=null && list.size() > 0) {
                    MaterialExtend materialExtend = list.get(0);
                    Material material = materialService.getMaterial(materialExtend.getMaterialId());
                    map.put("status", true);
                    map.put("material", material);
                } else {
                    map.put("status", false);
                }
            }
            res.code = 200;
            res.data = map;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }
    @GetMapping(value = "/checkIsCodeExist")
    @ApiOperation(value = "校验编码是否存在")
    public BaseResponseInfo checkIsCodeExist(@RequestParam("id") String id,
                                                @RequestParam("code") String code,
                                             HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<>();
        try {
//            if (StringUtils.isEmpty(code)){
//                map.put("status", false);
//            }else {
//                List<MaterialExtend> list = materialExtendService.checkIsCodeExist(id, code);
//                if(list!=null && list.size() > 0) {
//                    MaterialExtend materialExtend = list.get(0);
//                    Material material = materialService.getMaterial(materialExtend.getMaterialId());
//                    map.put("status", true);
//                    map.put("material", material);
//                } else {
//                    map.put("status", false);
//                }
//            }
            map.put("status", false);
            res.code = 200;
            res.data = map;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * @Author: wy
     * @Description: 获取商品的单位列表
     * @DateTime: 2022/8/17 20:28
     * @Params:
     * @Return
     */
    @GetMapping(value = "/getDetailByMaterialId")
    @ApiOperation(value = "单位下拉框")
    public BaseResponseInfo getDetailByMaterialId(@RequestParam("materialId") String materialId,
                                          HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<MaterialExtendVo4List> dataList = new ArrayList<MaterialExtendVo4List>();
            if(!StringUtil.isEmpty(materialId)) {
                dataList = materialExtendService.getDetailByMaterialId(materialId);
            }
            JSONObject outer = new JSONObject();
            //存放数据json数组
            JSONArray dataArray = new JSONArray();
            if (null != dataList) {
                for (MaterialExtendVo4List md : dataList) {
                    JSONObject item = new JSONObject();
                    item.put("meId", md.getId());
                    item.put("materialId", md.getMaterialId());
                    item.put("barCode", md.getBarCode());
                    item.put("commodityUnit", md.getCommodityUnit());
                    dataArray.add(item);
                }
            }
            outer.put("rows", dataArray);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }


}
